// 文件上传配置接口
export interface UploadConfig {
  allowedTypes: string[];
  maxSize: number; // 单位：字节
  maxSizeMB: number; // 单位：MB，用于显示
}

// 文件类型枚举
export enum FileType {
  IMAGE = "image",
  VIDEO = "video"
}

// 默认上传配置
export const UPLOAD_CONFIG: Record<FileType, UploadConfig> = {
  [FileType.IMAGE]: {
    allowedTypes: ["image/png", "image/jpg", "image/jpeg"],
    maxSize: 10 * 1024 * 1024, // 10MB
    maxSizeMB: 10
  },
  [FileType.VIDEO]: {
    allowedTypes: ["video/mp4", "video/mov", "video/avi"],
    maxSize: 50 * 1024 * 1024, // 50MB
    maxSizeMB: 50
  }
};

// 文件验证结果接口
export interface ValidationResult {
  isValid: boolean;
  error?: string;
}

// 文件验证函数
export const validateFile = (
  file: File,
  fileType: FileType
): ValidationResult => {
  const config = UPLOAD_CONFIG[fileType];

  // 检查文件类型
  if (!config.allowedTypes.includes(file.type)) {
    const allowedExtensions = config.allowedTypes.map(type => {
      const ext = type.split("/")[1];
      return ext === "jpeg" ? "jpg" : ext;
    });
    return {
      isValid: false,
      error: `文件格式不支持，请上传 ${allowedExtensions.join("、")} 格式的文件`
    };
  }

  // 检查文件大小
  if (file.size > config.maxSize) {
    return {
      isValid: false,
      error: `文件大小不能超过 ${config.maxSizeMB}MB`
    };
  }

  return { isValid: true };
};

// 获取文件类型
export const getFileType = (file: File): FileType | null => {
  if (file.type.startsWith("image/")) {
    return FileType.IMAGE;
  }
  if (file.type.startsWith("video/")) {
    return FileType.VIDEO;
  }
  return null;
};

// 通用文件验证函数
export const validateUploadFile = (file: File): ValidationResult => {
  const fileType = getFileType(file);

  if (!fileType) {
    return {
      isValid: false,
      error: "不支持的文件类型，请上传图片或视频文件"
    };
  }

  return validateFile(file, fileType);
};

// 更新配置函数
export const updateUploadConfig = (
  fileType: FileType,
  newConfig: Partial<UploadConfig>
) => {
  UPLOAD_CONFIG[fileType] = {
    ...UPLOAD_CONFIG[fileType],
    ...newConfig
  };
};

// 获取当前配置
export const getUploadConfig = (fileType: FileType): UploadConfig => {
  return { ...UPLOAD_CONFIG[fileType] };
};

// 获取所有配置
export const getAllUploadConfig = (): Record<FileType, UploadConfig> => {
  return { ...UPLOAD_CONFIG };
};

// 文件大小格式化
export const formatFileSize = (bytes: number): string => {
  if (bytes === 0) return "0 Bytes";

  const k = 1024;
  const sizes = ["Bytes", "KB", "MB", "GB"];
  const i = Math.floor(Math.log(bytes) / Math.log(k));

  return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + " " + sizes[i];
};

// 获取文件扩展名
export const getFileExtension = (filename: string): string => {
  return filename.slice(((filename.lastIndexOf(".") - 1) >>> 0) + 2);
};

// 检查文件扩展名是否在允许列表中
export const isAllowedExtension = (
  filename: string,
  fileType: FileType
): boolean => {
  const config = UPLOAD_CONFIG[fileType];
  const extension = getFileExtension(filename).toLowerCase();

  const allowedExtensions = config.allowedTypes.map(type => {
    const ext = type.split("/")[1];
    return ext === "jpeg" ? "jpg" : ext;
  });

  return allowedExtensions.includes(extension);
};
